package com.dgut.legalservice.service.impl;

import com.dgut.legalservice.dao.IAttachmentDao;
import com.dgut.legalservice.domain.attachment.Attachment;
import com.dgut.legalservice.domain.attachment.AttachmentGroup;
import com.dgut.legalservice.service.IAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AttachmentServiceImpl implements IAttachmentService {

    @Autowired
    IAttachmentDao attachmentDao;

    @Override
    @Transactional
    public int addAttachment(Attachment attachment) {
        return attachmentDao.insertAttachment(attachment);
    }

    @Override
    @Transactional
    public int addAttachmentGroup(AttachmentGroup attachmentGroup) {
        return attachmentDao.insertAttachmentGroup(attachmentGroup);
    }

    @Override
    @Transactional
    public int addAttachmentsAndGroup(List<Attachment> attachments, AttachmentGroup attachmentGroup) {
        attachmentDao.insertAttachmentGroup(attachmentGroup);
        for (Attachment attachment : attachments) {
            attachmentDao.insertAttachment(attachment);
            attachmentDao.insertAttGroupRef(attachmentGroup.getId(), attachment.getId());
        }
        return 1;
    }

    @Override
    @Transactional(readOnly = true)
    public Attachment findById(Integer id) {
        return attachmentDao.findAttachmentById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Attachment> findByGroupId(Integer groupId) {
        return attachmentDao.findAttachmentsByGroupId(groupId);
    }

    @Override
    @Transactional
    public int updateAttachment(Attachment attachment) {
        attachmentDao.updateAttachment(attachment);
        return 1;
    }

    @Override
    @Transactional
    public int deleteAttachment(Integer id) {
        attachmentDao.deleteAttachment(id);
        return 1;
    }

}
